package com.neteast.androidclient.newscenter.provider;

import java.util.HashMap;
import com.neteast.androidclient.newscenter.Global;
import com.neteast.androidclient.newscenter.db.DbHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;

public class LoginInfoProvider extends ContentProvider {
	private static HashMap<String, String> projectionMap;
	private static final int INFO = 1;
	private static final UriMatcher sUriMatcher;
	private DbHelper DBHelper;
	
	public static final String	AUTHORITY = "com.neteast.androidclient.newscenter";
	
	
	
	static{
		sUriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, "info", INFO);
		
		projectionMap=new HashMap<String, String>();
		projectionMap.put(LoginInfo._ID, LoginInfo._ID);
		projectionMap.put(LoginInfo.USERID, LoginInfo.USERID);
		projectionMap.put(LoginInfo.TOKEN, LoginInfo.TOKEN);
		
	}
	
	public static class LoginInfo implements BaseColumns{
		
		public static final Uri	CONTENT_URI	= Uri.parse("content://" + AUTHORITY + "/info");
		// 字段
		public static final String USERID = "userid";
		public static final String TOKEN = "token";
	}
	
	@Override
	public boolean onCreate() {
		DBHelper=new DbHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb=new SQLiteQueryBuilder();
		switch (sUriMatcher.match(uri)) {
		case INFO:
			qb.setTables(Global.TABLE_NAME_LOGIN_INFO);
			qb.setProjectionMap(projectionMap);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		SQLiteDatabase db = DBHelper.getReadableDatabase();
		Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, null);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		if (sUriMatcher.match(uri)!=INFO) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		delete(uri, null, null);
		ContentValues values;
		if (initialValues!=null) {
			values=new ContentValues(initialValues);
		} else {
			values=new ContentValues();
		}
		if (!values.containsKey(LoginInfo.USERID)) {
			throw new SQLException("参数错误，缺少userid");
		}
		if (!values.containsKey(LoginInfo.TOKEN)) {
			throw new SQLException("参数错误，缺少token");
		}
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		long rowId = db.insert(Global.TABLE_NAME_LOGIN_INFO,null, values);
		if (rowId>0) {
			Uri noteUri = ContentUris.withAppendedId(LoginInfoProvider.LoginInfo.CONTENT_URI, rowId);
			getContext().getContentResolver().notifyChange(noteUri, null);
			return noteUri;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		int count;
		switch (sUriMatcher.match(uri)) {
		case INFO:
			count=db.delete(Global.TABLE_NAME_LOGIN_INFO, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}

}
